package com.liyunc.demo.custom.fileupload.server;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.RandomAccessFile;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server {

    private DataOutputStream dos;
    private DataInputStream dis;
    private RandomAccessFile rad;

    private ServerSocket serverSocket = null;

    private JFrame frame;

    public Server() {
        frame = new JFrame("接收文件");
        try {
            serverSocket = new ServerSocket(8080);
            ExecutorService myExecutorService = Executors.newCachedThreadPool();
            Socket socket = null;
            while (true) {
                socket = serverSocket.accept();
                Socket finalSocket = socket;
                myExecutorService.execute(() -> {
                    try {
                        dis = new DataInputStream(finalSocket.getInputStream());
                        dos = new DataOutputStream(finalSocket.getOutputStream());
                        dis.readUTF();

                        int permit = JOptionPane.showConfirmDialog(frame, "是否接收文件");
                        if (permit == JOptionPane.YES_OPTION) {
                            String fileName = dis.readUTF();
                            dos.writeUTF("ok");
                            dos.flush();
                            File file = new File(fileName + ".temp");

                            rad = new RandomAccessFile(fileName + ".temp", "rw");

                            //获得文件大小
                            long size = 0;
                            if (file.exists() && file.isFile()) {
                                size = file.length();
                            }

                            dos.writeLong(size);
                            dos.flush();
                            long allSize = dis.readLong();
                            String rsp = dis.readUTF();

                            int barSize = (int) (allSize / 1024);
                            int barOffset = (int) (size / 1024);

                            //接收文件
                            if (rsp.equals("ok")) {
                                rad.seek(size);
                                int length;
                                byte[] buf = new byte[1024];
                                while (barOffset <= barSize) {
                                    length = dis.read(buf, 0, buf.length);
                                    rad.write(buf, 0, length);
                                    ++barOffset;
                                }
                            }
                            dis.close();
                            dos.close();
                            rad.close();
                            frame.dispose();
                            if (barOffset >= barSize) {
                                System.out.println("server+++++++++++++++filename" + fileName);
                                File target = new File(fileName);
                                boolean result = file.renameTo(target);
                                System.out.println("end ! rename result = " + result);
                            }
                        }
                    } catch (Exception e) {
                        System.out.println("传输失败");
                    }
                });
            }
        } catch (Exception e) {
            System.out.println("服务启动失败");
        }
    }
}
